import 'package:customer/extension/extension_widget.dart';
import 'package:customer/widgets/gyy_theme.dart';
import 'package:fluent_ui/fluent_ui.dart';

import '../../theme.dart';
import '../../three/datatable/datatable_header.dart';

class AttributeDropView extends StatefulWidget {
  final List<Map<String, dynamic>> source;
  Map<String, dynamic> selectedSub;
  final Function(bool value, Map<String, dynamic> data)? onSelect;
  final List<DatatableHeader> headers;

  AttributeDropView(this.source, this.selectedSub, this.headers,
      {Key? key, this.onSelect})
      : super(key: key);

  @override
  _GyyAttributeItemState createState() => _GyyAttributeItemState();
}

class _GyyAttributeItemState extends State<AttributeDropView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: _generateItems(),
    );
  }

  _generateItems() {
    List<Widget> widgets = [];
    for (var index = 0; index < widget.source.length; index++) {
      final data = widget.source[index];
      widgets.add(
        Container(
            decoration: widget.selectedSub["attributeCode"] ==
                    data["attributeCode"]
                ? BoxDecoration(
                    border: Border(
                        bottom:
                            BorderSide(color: AppTheme.accentColor, width: 1)),
                    color: AppTheme.accentColor,
                  )
                : BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                            color: AppTheme.accentColor.withOpacity(0.1),
                            width: 1))),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                sizeBoxH10,
                sizeBoxH10,
                Checkbox(
                    checked: widget.selectedSub["attributeCode"] ==
                        data["attributeCode"],
                    onChanged: (value) {
                      if (widget.onSelect != null) {
                        widget.onSelect!(value!, data);
                      }
                      if (widget.selectedSub == data) {
                        widget.selectedSub = {};
                      } else {
                        widget.selectedSub = data;
                      }
                      setState(() {});
                    }),
                ...widget.headers
                    .where((header) => header.show == true)
                    .map(
                      (header) => Expanded(
                        flex: header.flex,
                        child: Text(
                          "${data[header.value]}",
                          textAlign: header.textAlign,
                          style: data == widget.selectedSub ? whiteStyle : blackStyle,
                        ).padding(paddingH15),
                      ),
                    )
                    .toList()
              ],
            )),
      );
    }
    return widgets;
  }
}
